package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatSetting;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/22
 **/
public class RelevantInfoActivity extends BaseActivityImpl {

    @BindView(R.id.ll_root)
    LinearLayout llRoot;

    private ArrayList<GroupChatSetting.RelevantInfo> relevantInfos;

    public static void show(Context context, GroupChatSetting groupChatSetting, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, RelevantInfoActivity.class);
        intent.putExtra("groupChatSetting", groupChatSetting);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_relevant_info;
    }

    @Override
    public String getTitleStr() {
        return "动态信息关联";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GroupChatSetting groupChatSetting = (GroupChatSetting) getIntent().getSerializableExtra("groupChatSetting");

        relevantInfos = groupChatSetting.getRelevantInfo();

        if (relevantInfos != null && relevantInfos.size() > 0) {
            int size = relevantInfos.size();
            for (int i = 0; i < size; i++) {
                GroupChatSetting.RelevantInfo relevantInfo = relevantInfos.get(i);

                View item = inflater.inflate(R.layout.item_relevant_info, llRoot, false);
                TextView tvType = item.findViewById(R.id.tv_type);
                tvType.setText(relevantInfo.getTitle());

                SwitchButton switchButton = item.findViewById(R.id.switch_button);
                switchButton.setChecked(relevantInfo.isIs_switch());
                switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        relevantInfo.setIs_switch(isChecked);
                    }
                });
                llRoot.addView(item);

                if (i != size - 1) {
                    inflater.inflate(R.layout.divider, llRoot, true);
                }
            }
        } else {
            finish();
        }

        TextView tvConfirm = new TextView(mContext);
        tvConfirm.setBackgroundResource(R.drawable.selector_button_corner_green_180);
        tvConfirm.setText("确定");
        tvConfirm.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        tvConfirm.setTextColor(Color.WHITE);
        tvConfirm.setPadding(15, 5, 15, 5);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> params = new HashMap<>();
                params.put("id", groupChatSetting.getId());
                params.put("dynamic_list", relevantInfos);
                requestManager.request(requestManager.mMessageRetrofitService.editGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<Object>(mContext) {

                            @Override
                            public void onSuccess() {
                                super.onSuccess();

                                Intent intent = new Intent();
                                intent.putExtra("relevantInfos", relevantInfos);
                                setResult(RESULT_OK, intent);
                            }
                        });
            }
        });
        setOptions(tvConfirm);

    }
}








































































